import { subscribeWithSelector } from 'zustand/middleware'
import create from 'zustand'
import { useCreateContentStore } from '../../store'
import { fetchMixList } from '@/api/create/mix'
import { UserEntity } from '@/api/create/interface'
import Alert from '@/components/Alert'
import { t } from 'i18next'
import { getUrlParams } from '@/utils'
export type MixMtEntity = {
	id: string
	mode: number
	name: string
	members: Array<UserEntity>
}

type StateStore = {
	enable: boolean
	tDefault: number
	/**选中的预案*/
	planValue: string
	/**预案列表 */
	planOptions: Array<{
		label: string
		value: string
	}>
} & MixMtEntity

type IStore = StateStore & {
	update: (d: Partial<StateStore>) => void
	reset: () => void
	init: (_d: MixMtEntity | null) => void
	loadPlanData: () => void
	getMixData: (repeat: boolean) => Partial<MixMtEntity> | undefined
	checkData: () => boolean
}

const initialState: StateStore = {
	enable: true,
	/**默认智能混音 */
	mode: 1,
	/**混音成员 */
	members: [],
	/**预案名称 */
	name: '',
	id: '',
	tDefault: 0,
	planValue: '',
	planOptions: [{ label: t('不使用预案'), value: '0' }],
}
export const useMeetingMixStore = create<IStore>()(
	subscribeWithSelector((set, get) => ({
		...initialState,
		update: (d) => set({ ...d }),
		reset: () => set(initialState),
		init: (mix) => {
			if (mix && JSON.stringify(mix) !== '{}') {
				set({
					enable: true,
					id: mix.id ?? '',
					name: mix.name ?? '',
					mode: mix.mode ?? 1,
					members: mix.members ?? [],
					tDefault: 0, //0-为预设  1-为预案(此字段可不给)
				})
			} else {
				let flag = getUrlParams('templateMoid') || getUrlParams('meetingMoid') ? true : false
				/**
				 * 此处如果flag为true表明当前处于编辑会议或者编辑模板当中
				 * 创会默认开启混音
				 */
				if (!flag) {
					set({
						enable: true,
						/**默认智能混音 */
						mode: 1,
						/**混音成员 */
						members: [],
						/**预案名称 */
						name: '',
						id: '',
						tDefault: 0,
					})
				} else {
					set({
						enable: false,
						/**默认智能混音 */
						mode: 1,
						/**混音成员 */
						members: [],
						/**预案名称 */
						name: '',
						id: '',
						tDefault: 0,
					})
				}
			}
			get().loadPlanData()
		},
		loadPlanData: async () => {
			const preKey = useCreateContentStore.getState().preKey
			const isTemplate = useCreateContentStore.getState().isTemplate
			const res = await fetchMixList(isTemplate, preKey)
			if (res.success && res.data) {
				const planData = res.data
				let arr = planData ? planData.map((item) => ({ label: item.name, value: item.id })) : []
				arr.unshift({ label: t('不使用预案'), value: '0' })
				set({ planOptions: arr })
			}
		},

		getMixData: (repeat) => {
			/**重复会议的预案选择消息 因此此处的名称为空*/
			const { mode, name, members, enable, planValue } = get()
			if (!enable) {
				return undefined
			}
			let _members = members.map((value) => {
				return Object.assign({}, value, { conf_e164: value['conf_e164'] || value['parent_id'] })
			})
			return {
				mode: mode,
				name: repeat ? '' : planValue === '0' ? '' : name,
				members: mode === 1 ? [] : _members,
			}
		},
		checkData: () => {
			const { enable, mode, members } = get()
			let flag = true
			if (enable && mode === 2 && members.length === 0) {
				Alert(t('混音列表不能为空'))
				flag = false
			}
			return flag
		},
	}))
)
